//
//  WelcomViewController.swift
//  SinaWeibo
//
//  Created by mac on 15/8/12.
//  Copyright © 2015年 mac. All rights reserved.
//

import UIKit
import SDWebImage

class WelcomViewController: UIViewController {
//    图像底边约束
    private var iconBottomCons: NSLayoutConstraint?

    override func viewDidLoad() {
        super.viewDidLoad()
        prepareUI()
//        加载用户头像
        if let urlString = UserAccount.loadAccount()?.avatar_large{
            iconView.sd_setImageWithURL(NSURL(string: urlString)!)
        }
 
    }
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)

       iconBottomCons?.constant = UIScreen.mainScreen().bounds.height - iconBottomCons!.constant
//        动画
        UIView.animateWithDuration(1.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 5.0, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            self.view.layoutIfNeeded()
      
     print(self.iconBottomCons?.constant)
            }) { (_) -> Void in
                
                
        }
        
        
        
        
    }
 /// 准备界面
    private func prepareUI() {
        view.addSubview(backImageView)
        view.addSubview(iconView)
        view.addSubview(label)
//        自动布局
//        背景图片
        backImageView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("H:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview":backImageView]))
        view.addConstraints(NSLayoutConstraint.constraintsWithVisualFormat("V:|-0-[subview]-0-|", options: NSLayoutFormatOptions(rawValue: 0), metrics: nil, views: ["subview" : backImageView]))
//        头像
        iconView.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: view, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
//        设置头像的宽和高
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Width, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        view.addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.Height, relatedBy: NSLayoutRelation.Equal, toItem: nil, attribute: NSLayoutAttribute.NotAnAttribute, multiplier: 1.0, constant: 90))
        
        
        view.addConstraint(NSLayoutConstraint(item: view, attribute: NSLayoutAttribute.Bottom, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 180))
//        记录底边约束
        iconBottomCons = view.constraints.last
        print(iconBottomCons)
//        文字
        label.translatesAutoresizingMaskIntoConstraints = false
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.Top, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.Bottom, multiplier: 1.0, constant: 20))
        view.addConstraint(NSLayoutConstraint(item: label, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: iconView, attribute: NSLayoutAttribute.CenterX, multiplier: 1.0, constant: 0))
        
    }
    
    // MARK: - 懒加载控件
//    背景图片
    private lazy var backImageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
//    头像图片
    private lazy var iconView: UIImageView = {
       let iv = UIImageView(image: UIImage(named: "avatar_default_big"))
        iv.layer.masksToBounds = true
//        cornerRadius角半径
        iv.layer.cornerRadius = 45
        return iv
    }()
/// 消息文字
    private lazy var label: UILabel = {
       let label = UILabel()
        label.text = "Welcome to back"
        label.sizeToFit()
        return label
    }()
    
    
}
